<?php
namespace app\jiyun\controller\cargo;

use app\common\model\admin\service_config\ServiceConfigTabAccount;
use app\common\model\collect\OrderExpressGoodsModel;
use app\common\model\collect\OrderExpressModel;
use app\common\model\jiyun\base\ChannelModel;
use app\common\model\jiyun\OrderModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use think\admin\Controller;
use think\facade\Validate;

/**
 * 打印面单
 * @Description
 * @package app\jiyun\controller\cargo
 * @author wangqinghui
 * @date 2024-12-04 10:22
 */
class OrderPrint extends Controller
{
    /**
     * 打印面单
     * @Description
     * @auth true
     * @menu true
     * @author wangqinghui
     * @date 2024-12-04 15:43
     * @return void
     */
    public function index()
    {
        $this->fetch();
    }

    /**
     * 搜索订单信息
     * @auth true
     * @Description
     * @author wangqinghui
     * @date 2024-12-04 15:43
     * @return void
     */
    public function searchOrder()
    {
        $express_no = $this->request->post('express_no');
        if(empty($express_no)){
            $this->error('请输入快递单号或者合箱单号');
        }

        // 查询拣货单信息
        $map = [
            ['express_no',    '=', $express_no],
            ['status',  '=', OrderExpressModel::STATUS_IN_STORE]
        ];
        $pack_info = OrderExpressModel::mk()->where($map)->find();
        if(empty($pack_info)){
            $this->error('快递单号或者合箱单号信息有误');
        }
        $order_express_ids = [$pack_info['id']];

        // 判断是否是合箱订单
        if(!empty($pack_info['is_merge'])){
            $order_express_ids = OrderExpressModel::mk()->where('pid', $pack_info['id'])->column('id');
        }else if(!empty($pack_info['pid'])){
            $map = [
                ['id', '=', $pack_info['pid']],
                ['status',  '=', OrderExpressModel::STATUS_IN_STORE],
                ['is_merge',  '=', 1]
            ];
            $pack_info = OrderExpressModel::mk()->where($map)->find();
            if(empty($pack_info)){
                $this->error('快递单号信息有误,合箱不存在');
            }
            $order_express_ids = OrderExpressModel::mk()->where('pid', $pack_info['id'])->column('id');
        }
        // 获取订单信息
        $order_id = $pack_info['jiyun_order_id'] ?? 0;
        $order_data = [];
        if(!empty($order_id)){
            $map = [
                ['id', '=', $order_id],
            ];
            $order_info = OrderModel::mk()->where($map)->find();
            $order_data = !empty($order_info) ? $order_info->toArray() : [];
        }
        if(empty($order_data)){
            $this->error('订单信息有误');
        }
        if($order_data['status'] == OrderModel::STATUS_NO_IN){
            $this->error('订单包裹还未全入库');
        }
        // 获取仓库信息
        $order_data['warehouse_name'] = '';
        if(!empty($order_data['warehouse_id'])){
            $map = [
                ['id', '=', $order_data['warehouse_id']]
            ];
            $warehouse_info = WarehouseModel::mk()->where($map)->find();
            $order_data['warehouse_name'] = $warehouse_info['warehouse_c_name'] ?? '';
        }
        // 获取渠道信息
        $order_data['channel_name'] = '';
        if(!empty($order_data['channel_id'])){
            $map = [
                ['id', '=', $order_data['channel_id']]
            ];
            $channel_info = ChannelModel::mk()->where($map)->find();
            $order_data['channel_name'] = $channel_info['title'] ?? '';
        }
        // 快递名称
        $order_data['express_name'] = '';
        if(!empty($channel_info['express_id'])){
            $map = [
                ['id', '=', $channel_info['express_id']]
            ];
            $order_data['express_name'] = ServiceConfigTabAccount::mk()->where($map)->value('alias_name');
        }
        // 国际物流单号
        $order_data['foreign_express_no'] = $pack_info['foreign_express_no'] ?? '';
        // 获取商品信息
        $map = [
            ['order_express_id',    'in', $order_express_ids]
        ];
        $field = 'id,order_express_id,name,num,spec_value,image';
        $pack_goods = OrderExpressGoodsModel::mk()->field($field)->where($map)->select()->toArray();
        // 获取拣货商品信息
        $this->success('查询成功', ['order_info' => $order_data, 'pack_goods' => $pack_goods, 'pack_info' => $pack_info]);
    }

    /**
     * 打印面单
     * @Description
     * @auth true
     * @author wangqinghui
     * @date 2024-12-06 18:07
     * @return void
     */
    public function printSheet()
    {
        $post_data = $this->request->post();
        $rule = [
            'overseas_warehouse|海外仓库'  => 'require|integer|gt:0',
            'length|长'          => 'float|between:0.01,1000000',
            'wide|宽'           => 'float|between:0.01,1000000',
            'height|高'          => 'float|between:0.01,1000000',
            'total_weight|重量'        => 'require|float|between:0.01,1000000',
            'express_id|包裹号'  => 'require|integer|gt:0',
        ];
        $validate = Validate::rule($rule);
        if(!$validate->check($post_data)) {
            $this->error($validate->getError());
        }
        // 获取快递单信息
        $express_info = OrderExpressModel::mk()->where('id', $post_data['express_id'])->find();
        if(empty($express_info)){
            $this->error('快递单号或者合箱单号信息有误');
        }
        if(!empty($express_info['sheet_url'])){
            $this->success('获取单号成功', ['sheet_url' => $express_info['sheet_url']]);
        }
        // 获取快递渠道,打印面单
        $express_sheet_url = 'http://ssh.yjkuajing.com/upload/20241206/029ba79011af98ceab5acd99160d7b24.pdf';
        $foreign_express_no = '12422554';

        // 更新快递包裹信息
        $update_data = [
            'warehouse_abroad_id'   => $post_data['overseas_warehouse'],
            'length'                => $post_data['length'],
            'width'                 => $post_data['wide'],
            'height'                 => $post_data['height'],
            'weight'                => $post_data['total_weight'],
            'sheet_url'             => $express_sheet_url,
            'foreign_express_no'     => $foreign_express_no,
        ];
        $count = OrderExpressModel::mk()->where('id', $post_data['express_id'])->update($update_data);
        $this->success('获取单号成功', ['sheet_url' => $express_sheet_url]);
    }
}